<?php
namespace App\Admin\Controllers;

use App\Models\Config;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

/**
 * Created By FreeAbrams
 * Date: 2020/11/20
 */
class ReceptionMachineController
{
	// 打开页面时查询是否有 reception_machine_id 打印机列表
	// 没有则新建后返回，有则返回
	protected $reception_machine_id = [];
	
	public function index(Content $content)
	{
		$config = Config::where('store_id', Admin::user()->store_id)
			->where('name', 'reception_machine_id')
			->first();
		
		// 没有则创建一条数据
		if(empty($config)){
			$reception_machine_id = new Config();
			$reception_machine_id->store_id = Admin::user()->store_id;
			$reception_machine_id->name = 'reception_machine_id';
			$reception_machine_id->save();
			$reception_machine_id->refresh();
			
			$data = $reception_machine_id->toArray();
		} else {
			
			$data = $config->toArray();
		}
		
		return $content
			->header('打印机设置')
			->row(view('admin.store.reception_machine',
				['data' => $data]));
		
	}
	
	public function EditReceptionMachine(Content $content,Request $request)
	{
		$data = $request->all();

		$config = Config::where('store_id', Admin::user()->store_id)
			->where('name', 'reception_machine_id')->first();
		$config->value = $data['reception_machine'];
		$config->save();
		
		return redirect()->route('receptionMachine.index');
	}
}